﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.DataAccess.Configurations
{
    public class ItemConfig : EntityTypeConfiguration<Item>
    {
        public ItemConfig()
        {
            #region Relaionships

            HasMany(p => p.Categories)
                    .WithMany(p => p.Items)
                    .Map(p =>
                    {
                        p.ToTable("CategoryItems");
                        p.MapLeftKey("ItemId");         // The "Left" key HAS to be for this entity first, THEN the other entity key*
                        p.MapRightKey("CategoryId");    // This is the other entity (the other end)
                    });

            #endregion

            #region Properties

            HasKey(p => p.Id);

            Property(p => p.Id)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(32)
                    .IsRequired();

            Property(p => p.IsVisible)
                    .HasColumnType("bit")
                    .IsRequired();

            Property(p => p.Title)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(256)
                    .IsRequired();

            Property(p => p.Desc)
                    .HasColumnName("Description")
                    .HasColumnType("nvarchar")
                    .HasMaxLength(2048)
                    .IsOptional();

            Property(p => p.Price)
                    .HasColumnType("money")
                    .HasPrecision(6,2)
                    .IsOptional();

            Property(p => p.IsInStock)
                    .HasColumnType("bit")
                    .IsOptional();

            Property(p => p.ImageUrl)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(256)
                    .IsOptional();

            Property(p => p.ImageAltText)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(256)
                    .IsOptional();

            // Property(p => p.DisplayOrder)        // defined in the original kit but never used
            //        .HasColumnType("int")
            //        .IsOptional();

            #endregion
        }
    }
}
